import ElementUI from 'element-ui'
import serverConf from "./serverConf"

function initWebSocket(e) {
    let WS_API = `ws://${serverConf.baseHost}:${serverConf.basePort}`
    const wsUri = WS_API + `/ws/${e}/`
    this.socket = new WebSocket(wsUri)
    this.socket.onerror = webSocketOnError
    this.socket.onclose = closeWebsocket
    this.socket.onopen = webSocketOnOpen
}
function webSocketOnOpen(e) {
    ElementUI.Message.success('WebSocket连接成功')
}
function webSocketOnError(e) {
    ElementUI.Message.error('WebSocket连接发生错误')
}
function closeWebsocket() {
    ElementUI.Message.error('WebSocket连接已关闭')
}
function close() {
    this.socket.close() // 关闭 websocket
}
function webSocketSend(agentData) {
    this.socket.send(JSON.stringify(agentData))
}
export default {
    initWebSocket, close, webSocketSend
}